package com.fdd.op.demo;

import com.fdd.op.sdk.ApiException;
import com.fdd.op.sdk.DefaultFddClient;
import com.fdd.op.sdk.FileItem;
import com.fdd.op.sdk.constants.Constants;
import com.fdd.op.sdk.internal.token.FddTokenStore;
import com.fdd.op.sdk.internal.token.stroe.CacheTokenStoreImpl;
import com.fdd.op.sdk.internal.util.json.JSONWriter;
import com.fdd.op.sdk.model.dto.*;
import com.fdd.op.sdk.request.api.account.CreateAccountRequest;
import com.fdd.op.sdk.request.api.company.CreateCompanyRequest;
import com.fdd.op.sdk.request.api.contract.AbolishRequest;
import com.fdd.op.sdk.request.api.contract.ApplyEvidenceRequest;
import com.fdd.op.sdk.request.api.contract.GetDocByContractIdRequest;
import com.fdd.op.sdk.request.api.contract.ShutSignTaskRequest;
import com.fdd.op.sdk.request.api.contract.manual.SignRequest;
import com.fdd.op.sdk.request.api.contract.sign.CreateAndPushRequest;
import com.fdd.op.sdk.request.api.contract.sign.PushRequest;
import com.fdd.op.sdk.request.api.contract.signtask.CreateRequest;
import com.fdd.op.sdk.request.api.file.UploadRequest;
import com.fdd.op.sdk.request.api.verify.company.UrlRequest;
import com.fdd.op.sdk.response.api.account.CreateAccountResponse;
import com.fdd.op.sdk.response.api.company.CreateCompanyResponse;
import com.fdd.op.sdk.response.api.contract.AbolishResponse;
import com.fdd.op.sdk.response.api.contract.ApplyEvidenceResponse;
import com.fdd.op.sdk.response.api.contract.GetDocByContractIdResponse;
import com.fdd.op.sdk.response.api.contract.ShutSignTaskResponse;
import com.fdd.op.sdk.response.api.contract.manual.SignResponse;
import com.fdd.op.sdk.response.api.contract.sign.CreateAndPushResponse;
import com.fdd.op.sdk.response.api.contract.sign.PushResponse;
import com.fdd.op.sdk.response.api.contract.signtask.CreateResponse;
import com.fdd.op.sdk.response.api.file.UploadResponse;
import com.fdd.op.sdk.response.api.verify.company.UrlResponse;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class Demo {
    //<H3>name</Name>
    private static String serverUrl = "http://local-op2.fabigbig.com:6442";
    private static final String appId = "721522";
    private static final String appKey = "hVpnGJPciVuAc1VXJk9CV7mbZCZejdaO";


    /**
     * 最小化demo
     * @param args
     * @throws ApiException
     */
    public static void main(String[] args) throws ApiException {
        //serverUrl = "http://127.0.0.1";
        FddTokenStore tokenStore = new CacheTokenStoreImpl();
        DefaultFddClient fddClient = new DefaultFddClient(serverUrl, appId, appKey, Constants.AUTH_OAUTH, 10000, 120000);
        fddClient.tokenStore(tokenStore);
        Demo demo = new Demo();

        //demo.createAndPush(fddClient);
        demo.test(fddClient);
        //demo.createAccount(fddClient);

        //创建企业
        //demo.createCompany(fddClient);

        //个人认证
        //demo.verifyPerson(fddClient);

        //企业实名认证
        //demo.verifyCompany(fddClient);

        //文件上传
        //demo.upload(fddClient);

        //创建签署任务    20230306_contractPdf_287899549312389120   resp:{"code":0,"data":{"docId":"287903635495620608"},"message":"successed","timestamp":"1678071975915"}
        //demo.create(fddClient);

        //推送签署任务
        //demo.push(fddClient);

        //签署成功后 关闭签署任务
        //demo.shutSignTask(fddClient);

        //文档下载
        //demo.getDocByContractId(fddClient);

        //文档作废
        //demo.abolish(fddClient);

        //申请出证报告
        //demo.applyEvidence(fddClient);

    }
//{"sign_url_list":[{"notify_no":"999990","short_url":"http://172.19.128.217:32258/f162jXbwUs"}]}
    public void test(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, false);
        SignRequest request = new SignRequest();
        request.setDocId("287903635495620608");
        request.setUrlTimeOut(120);
        request.setAccessType(1);
        request.setAccessCode("12345T");
        //request.setSignerAccountId("230703209230323712");
        request.setSignerName("潘金生");

        request.setMobile("15677148827");
        //request.setIsAllowReject(1);
        request.setNotifyNo("15561616");

        List<DocSignInfoDTO> docSignInfoDTOList = new ArrayList<>();
        DocSignInfoDTO docSignInfoDTO = new DocSignInfoDTO();
        docSignInfoDTO.setFileId("20230306_contractPdf_287899549312389120");
        docSignInfoDTOList.add(docSignInfoDTO);
        //request.setDocSignInfoDTO(docSignInfoDTOList);
        SignResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            System.out.println(writer.write(response.getUrl()));
        }
    }

    public void createAndPush(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, false);
        CreateAndPushRequest request = new CreateAndPushRequest();
        request.setDocTitle("001_TongWeb7快速使用手册");
        request.setNotifyUrl("http://127.0.0.1/test");
        List<DocInfoDTO> docList = new ArrayList<>();
        DocInfoDTO docInfoDTO = new DocInfoDTO();
        docInfoDTO.setFileId("20230306_contractPdf_287899549312389120");
        docList.add(docInfoDTO);
        request.setDocList(docList);

        List<ContractCreateAndPushSignInfoDTO> signerInfos = new ArrayList<>();
        ContractCreateAndPushSignInfoDTO contractCreateAndPushSignInfoDTO = new ContractCreateAndPushSignInfoDTO();
        contractCreateAndPushSignInfoDTO.setSignerAccountId("230703209230323712");
        contractCreateAndPushSignInfoDTO.setNotifyNo("112211471414145e");
        contractCreateAndPushSignInfoDTO.setPriority(1);
        signerInfos.add(contractCreateAndPushSignInfoDTO);

        request.setSignerInfos(signerInfos);

        request.setSenderMobile("15677148827");
        //request.setCompanyId("174893282030809088");

        CreateAndPushResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            System.out.println(writer.write(response.getData()));
        }
    }

    public void applyEvidence(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, false);
        ApplyEvidenceRequest request = new ApplyEvidenceRequest();
        request.setDocId("242590153926733824");
        request.setNotifyUrl("http://127.0.0.1/test");
        request.setNotifyNo("1234567890");
        ApplyEvidenceResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            System.out.println(writer.write(response.getData()));
        }
    }

    /**
     * 个人实名认证
     * @param fddClient
     * @throws ApiException
     */
    public void verifyPerson(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, false);
        com.fdd.op.sdk.request.api.verify.person.UrlRequest request = new com.fdd.op.sdk.request.api.verify.person.UrlRequest();
        request.setTpAccountId("panjinsheng");
        request.setVerifiedWay(0);
        request.setVerifiedType(1);

        request.setNotifyUrl("http://127.0.0.1/test");

        com.fdd.op.sdk.response.api.verify.person.UrlResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            System.out.println(response.getData());
            System.out.println(writer.write(response.getData()));
        }
    }

    /**
     * 企业实名认证
     * @param fddClient
     * @throws ApiException
     */
    public void verifyCompany(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, false);
        UrlRequest request = new UrlRequest();

        request.setTpOrgId("tpOrgId");
        request.setVerifiedWay(0);

        CompanyInfoDTO companyInfoDTO = new CompanyInfoDTO();
        companyInfoDTO.setCompanyName("接口创建企业");
        request.setCompanyInfoDTO(companyInfoDTO);

        request.setIsRepeatVerified(1);
        request.setTpAccountId("panjinsheng");
        request.setNotifyUrl("http://127.0.0.1/test");

        UrlResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            System.out.println(response.getData());
            System.out.println(writer.write(response.getData()));
        }
    }

    /**
     * 文档下载
     * @param fddClient
     * @throws ApiException
     */
    public void getDocByContractId(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, false);
        GetDocByContractIdRequest request = new GetDocByContractIdRequest();
        request.setDocId("242590153926733824");

        GetDocByContractIdResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            //{"fileUrl":"http://local-op2.fabigbig.com/base/contract/file/apiDownload?uuid=2b8a1ca369084ada8f0b40f9003b6e26","zipFileSha":"6e63c656948946c76bfef08efbea0e725a7758413842bec2d1dc1424fd285001"}
            System.out.println(writer.write(response.getData()));
        }
    }

    /**
     *
     * @param fddClient
     * @throws ApiException
     */
    public void abolish(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, true);
        AbolishRequest request = new AbolishRequest();
        request.setDocId("242590153926733824");
        request.setReason("文档作废");
        request.setTpAccountId("panjinsheng");
        AbolishResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            //{"doc_id":"242611006194069504","sign_infos":[{"account_id":"230703209230323712","url":"http://172.19.128.217:32258/belPuO4zEq"}]}
            System.out.println(writer.write(response.getData()));
        }
    }

    /**
     * 关闭签署任务
     */
    public void shutSignTask(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, true);
        ShutSignTaskRequest request = new ShutSignTaskRequest();
        //request.setDocId("242590153926733824");
        ShutSignTaskResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            System.out.println(response.getData());
            System.out.println(writer.write(response.getData()));
        }
    }

    /**
     * 推送签署任务
     * @param fddClient
     * @throws ApiException
     */
    public void push(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, true);
        PushRequest request = new PushRequest();
        request.setDocId("285829530298417152");
        List<PushSignInfoDTO> signerInfos = new ArrayList<>();
        PushSignInfoDTO pushSignInfoDTO = new PushSignInfoDTO();
        pushSignInfoDTO.setSignerAccountId("230703209230323712");
        pushSignInfoDTO.setNotifyNo("999990");
        pushSignInfoDTO.setPriority(1);

        signerInfos.add(pushSignInfoDTO);
        request.setSignerInfos(signerInfos);

        PushResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            //{"sign_url_list":[{"notify_no":"99999","short_url":"http://172.19.128.217:32258/Fxqgnz6PEq"}]}
            System.out.println(writer.write(response.getData()));
        }
    }

    /**
     * 创建文档签署任务
     * @param fddClient
     */
    public void create(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, true);
        CreateRequest request = new CreateRequest();
        request.setDocTitle("新同学入学20221101指引");
        List<DocInfoDTO> docList = new ArrayList<>();
        DocInfoDTO docInfoDTO = new DocInfoDTO();
        docInfoDTO.setFileId("20230306_contractPdf_287899549312389120");
        docList.add(docInfoDTO);
        request.setDocList(docList);

        request.setNotifyUrl("http://127.0.0.1/test");
        request.setSenderAreaCode("+86");
        request.setSenderMobile("15677148827");
        //request.setCompanyId("1623880420619816961");
        CreateResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            //{"doc_id":"242590153926733824"}  {"doc_id":"242593647462207488"}
            System.out.println(writer.write(response.getData()));
        }
    }

    /**
     *
     * 合同文件上传
     * @param fddClient
     * @throws ApiException
     */
    public void upload(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, true);
        UploadRequest request = new UploadRequest();
        request.setFileName("001_TongWeb7快速使用手册.pdf");
        request.setFileItem("file", new FileItem("D:\\BaiduNetdiskDownload\\001_TongWeb7快速使用手册.pdf"));
        //request.setFileUrl("http://www.xcxmf.cn:8888/oss_ols/TMYW1669103292483.pdf");
        //request.setFileUrl("https://contract-intelligent-dev-1256586296.cos.ap-shanghai.myqcloud.com/cip-test/202208/26khwhi9zwkg7wxzgf1i4u.docx?q-sign-algorithm=sha1&q-ak=AKIDeNyLVTtp575vXPdZx6EAyh8Ts2yVo8GJ&q-sign-time=1671438445%3B1671474445&q-key-time=1671438445%3B1671474445&q-header-list=host&q-url-param-list=&q-signature=ad26b882755eecd1f2bc604788edb09f73d21adb");
        UploadResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            //{"file_id":"20230306_contractPdf_287899549312389120"}
            System.out.println(writer.write(response.getData()));
        }
    }

    /**
     * 创建用户
     */
    public void createAccount(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, true);
        CreateAccountRequest request = new CreateAccountRequest();
        request.setUserName("潘金生");
        request.setMobile("17788721197");
        CreateAccountResponse response = fddClient.execute(request);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", response.getCode(), response.getMessage()));
        if(response != null && "0".equals(response.getCode())){
            System.out.println(response.getData());
            System.out.println(writer.write(response.getData()));
        }
    }

    /**
     * 创建企业
     */
    public void createCompany(DefaultFddClient fddClient) throws ApiException {
        JSONWriter writer = new JSONWriter(false, false);
        CreateCompanyRequest createCompanyRequest = new CreateCompanyRequest();
        createCompanyRequest.setCompanyName("接口创建测试企业1234");
        createCompanyRequest.setAdminName("潘金生");
        //createCompanyRequest.setTpOrgId("123abc1");
        //createCompanyRequest.setTpAccountId("dasdw123");
        createCompanyRequest.setAdminMobile("15677148827");
        CreateCompanyResponse createCompanyResponse = fddClient.execute(createCompanyRequest);
        System.out.println(MessageFormat.format("code:{0}, message:{1}", createCompanyResponse.getCode(), createCompanyResponse.getMessage()));
        if(createCompanyResponse != null && "0".equals(createCompanyResponse.getCode())){
            //{"accountId":"242583877892685824","companyId":"1587258553074884610"}
            System.out.println(writer.write(createCompanyResponse.getData()));
        }
    }

}
